package Greedy;

/**
 * @ClassName MonotoneIncreasingDigits
 * @Description TODO
 * @Author lenovo
 * @Date 2023-06-27 14:43
 * @Version 1.0
 * @Comment Magic. Do not touch.
 * If this comment is removed. the program will blow up
 */
public class MonotoneIncreasingDigits {

    /**
     * 示例 1:
     * <p>
     * 输入: n = 10
     * 输出: 9
     * 示例 2:
     * <p>
     * 输入: n = 1234
     * 输出: 1234
     * 示例 3:
     * <p>
     * 输入: n = 352
     * 输出: 299
     *
     * @param N
     * @return
     */
    public int monotoneIncreasingDigits(int N) {
        String[] arrays = (N + "").split("");
        int start = arrays.length;
        for (int i = arrays.length - 1; i > 0; i--) {
            if (Integer.valueOf(arrays[i - 1]) > Integer.valueOf(arrays[i])) {
                arrays[i - 1] = String.valueOf(Integer.valueOf(arrays[i - 1]) - 1);
                start = i;
            }
        }
        for (int i = start; i < arrays.length; i++) {
            arrays[i] = 9+"";
        }
        String join = String.join("", arrays);
        return Integer.valueOf(join);
    }

    public static void main(String[] args) {
        MonotoneIncreasingDigits monotoneIncreasingDigits = new MonotoneIncreasingDigits();
        System.out.println(monotoneIncreasingDigits.monotoneIncreasingDigits(352));
    }
}